package com.ptp.backendcertms.basiccompnents.data.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.CertificatePo;
import com.ptp.backendcertms.basiccompnents.data.dao.mapper.ICertificateMapper;
import com.ptp.backendcertms.basiccompnents.data.dao.service.ICertificateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.ptp.backendcertms.api.internal.constant.DateTypeConstant.DATE_TIME_PATTERN_SHORT_MATTER;

/**
 * 证书表 服务实现类
 */
@Service
public class CertificateServiceImpl extends ServiceImpl<ICertificateMapper, CertificatePo> implements ICertificateService {

    @Override
    public IPage<CertificatePo> pageList(CertificatePo queryPo, Integer current, Integer pageSize) {
        LambdaQueryWrapper<CertificatePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CertificatePo::getDelFlag, true);
        if (StringUtils.isNotBlank(queryPo.getOrganizationName())) {
            queryWrapper.like(CertificatePo::getOrganizationName, queryPo.getOrganizationName());
        }
        if (StringUtils.isNotBlank(queryPo.getCertificateNo())) {
            queryWrapper.eq(CertificatePo::getCertificateNo, queryPo.getCertificateNo());
        }
        queryWrapper.select(CertificatePo::getCertificateNo, CertificatePo::getOrganizationName,
                CertificatePo::getCertificationProject, CertificatePo::getDueDate,
                CertificatePo::getCertificateStatus, CertificatePo::getSerialNo);
        queryWrapper.orderByDesc(CertificatePo::getId);
        IPage<CertificatePo> page = new Page<>(current, pageSize);
        return page(page, queryWrapper);
    }

    @Override
    public CertificatePo queryByCertNo(String certNo) {
        return getOne(Wrappers.<CertificatePo>lambdaQuery()
                .eq(CertificatePo::getDelFlag, true)
                .eq(CertificatePo::getCertificateNo, certNo));
    }

    @Override
    public void saveOrUpdateCertificateInfo(CertificatePo entity) {
        if (Objects.isNull(entity.getId())) {
            entity.setCreateTime(LocalDateTime.now().format(DATE_TIME_PATTERN_SHORT_MATTER));
            save(entity);
        }
        updateById(entity);
    }

    @Override
    public IPage<CertificatePo> pageListMc(CertificatePo queryPo, String dueStartDate, String dueEndDate, String issueStartDate, String issueEndDate, Integer current, Integer pageSize) {
        LambdaQueryWrapper<CertificatePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CertificatePo::getDelFlag, true);
        if(StringUtils.isNotBlank(queryPo.getOrganizationName())){
            queryWrapper.like(CertificatePo::getOrganizationName, queryPo.getOrganizationName());
        }
        if (StringUtils.isNotBlank(queryPo.getSocialCreditCode())) {
            queryWrapper.eq(CertificatePo::getSocialCreditCode, queryPo.getSocialCreditCode());
        }
        if(StringUtils.isNotBlank(queryPo.getCertificateNo())){
            queryWrapper.eq(CertificatePo::getCertificateNo, queryPo.getCertificateNo());
        }
        if(Objects.nonNull(queryPo.getCertificateStatus())){
            queryWrapper.eq(CertificatePo::getCertificateStatus, queryPo.getCertificateStatus());
        }
        if (StringUtils.isNotBlank(dueStartDate) && StringUtils.isNotBlank(dueEndDate)) {
            queryWrapper.ge(CertificatePo::getDueDate, dueStartDate);
            queryWrapper.le(CertificatePo::getDueDate, dueEndDate);
        }
        if (StringUtils.isNotBlank(issueStartDate) && StringUtils.isNotBlank(issueEndDate)) {
            queryWrapper.ge(CertificatePo::getIssueDate, issueStartDate);
            queryWrapper.le(CertificatePo::getIssueDate, issueEndDate);
        }
        queryWrapper.select(CertificatePo::getSerialNo,
                CertificatePo::getCertificateNo, CertificatePo::getOrganizationName,
                CertificatePo::getSocialCreditCode, CertificatePo::getDueDate,
                CertificatePo::getCertificateStatus, CertificatePo::getIssueDate);
        queryWrapper.orderByDesc(CertificatePo::getId);
        IPage<CertificatePo> page = new Page<>(current, pageSize);
        return page(page, queryWrapper);
    }

    @Override
    public CertificatePo queryBySerialNo(String serialNo) {
        LambdaQueryWrapper<CertificatePo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CertificatePo::getDelFlag, true);
        queryWrapper.eq(CertificatePo::getSerialNo, serialNo);
        return getOne(queryWrapper);
    }

    @Override
    public void deleteBySerialNo(CertificatePo updatePo) {
        LambdaUpdateWrapper<CertificatePo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(CertificatePo::getDelFlag, false);
        updateWrapper.eq(CertificatePo::getSerialNo, updatePo.getSerialNo());
        baseMapper.update(updatePo,updateWrapper);
    }

    @Override
    public void updateCertificate(CertificatePo updatePo) {
        LambdaUpdateWrapper<CertificatePo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(CertificatePo::getSerialNo, updatePo.getSerialNo());
        baseMapper.update(updatePo,updateWrapper);
    }
}
